//
//  UserObj.m
//  seer
//
//  Created by Lin Eidan on 14-5-28.
//  Copyright (c) 2014年 haomee. All rights reserved.
//

#import "UserObj.h"
#import "config.h"

#define LocalUserObjKey     @"LocalUserObjKey"

@implementation UserObj

+ (UserObj *)sharedInstance
{
    static UserObj *_sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        if ([kUser objectForKey:LocalUserObjKey]) {
            _sharedInstance = [NSKeyedUnarchiver unarchiveObjectWithData:[kUser objectForKey:LocalUserObjKey]];
        }else{
            _sharedInstance = [[UserObj alloc] init];
        }
    });
    return _sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
        [self handleToDefault];
    }
    return self;
}

#pragma -mark other

//真正合法的登录
-(BOOL)isLegalLogin{
    if (self.isLogin && self.uid && self.accessKey && self.uid.length > 0 && self.accessKey.length > 0) {
        return YES;
    }
    return NO;
}

//真正合法的VIP
-(BOOL)isLegalVip{
    if ([self isLegalLogin] && self.isVip) {
        return YES;
    }
    return NO;
}


-(void)logout{
    [self handleToDefault];
    [self saveUserToLocal];
}

-(void)handleToDefault{
    self.isLogin = NO;
    self.isVip = NO;
    self.username = DefaultUserName;
    self.headId = DefaultUserHeaderId;
    self.campId = DefaultUserCampId;
    self.headPicSrc = @"";
    self.campPicSrc =@"";
    self.qqId = @"0";
    self.sinaId =@"0";
    self.vipStartDate = nil;
    self.vipOverData = nil;
    self.qqNickName = @"";
    self.sinaNickName = @"";
}


#pragma -mark saveData

-(void)firstInstallAPPHandle{
    if (![kUser objectForKey:LocalUserObjKey]) {  //第一次安装APP，就把他存在本地.
        [self saveUserToLocal];
    }
}

-(void)saveUserToLocal{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:self];
        [kUser setObject:archiveData forKey:LocalUserObjKey];
        [kUser synchronize];
        
        dispatch_async(dispatch_get_main_queue(), ^{
        });
        
    });
    
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.uid forKey:@"1"];
    [aCoder encodeObject:self.username forKey:@"2"];
    [aCoder encodeObject:self.accessKey forKey:@"3"];
    [aCoder encodeObject:self.phoneNum forKey:@"4"];
    [aCoder encodeBool:self.isLogin forKey:@"5"];
    [aCoder encodeBool:self.isVip forKey:@"6"];
    [aCoder encodeObject:self.headId forKey:@"7"];
    [aCoder encodeObject:self.campId forKey:@"8"];
    [aCoder encodeObject:self.headPicSrc forKey:@"9"];
    [aCoder encodeObject:self.campPicSrc forKey:@"10"];
    [aCoder encodeObject:self.medalsDisplayArray forKey:@"11"];
    [aCoder encodeObject:self.qqId forKey:@"12"];
    [aCoder encodeObject:self.sinaId forKey:@"13"];
    [aCoder encodeObject:self.qqNickName forKey:@"14"];
    [aCoder encodeObject:self.sinaNickName forKey:@"15"];
    [aCoder encodeObject:self.vipStartDate forKey:@"16"];
    [aCoder encodeObject:self.vipOverData forKey:@"17"];
    [aCoder encodeInteger:self.lastSysMessageCount forKey:@"18"];
    [aCoder encodeInteger:self.lastMedalCount forKey:@"19"];
    [aCoder encodeInt:self.coins forKey:@"20"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self){
        self.uid = [aDecoder decodeObjectForKey:@"1"];
        self.username = [aDecoder decodeObjectForKey:@"2"];
        self.accessKey = [aDecoder decodeObjectForKey:@"3"];
        self.phoneNum = [aDecoder decodeObjectForKey:@"4"];
        self.isLogin = [aDecoder decodeBoolForKey:@"5"];
        self.isVip = [aDecoder decodeBoolForKey:@"6"];
        self.headId = [aDecoder decodeObjectForKey:@"7"];
        self.campId = [aDecoder decodeObjectForKey:@"8"];
        self.headPicSrc = [aDecoder decodeObjectForKey:@"9"];
        self.campPicSrc = [aDecoder decodeObjectForKey:@"10"];
        self.medalsDisplayArray = [aDecoder decodeObjectForKey:@"11"];
        self.qqId = [aDecoder decodeObjectForKey:@"12"];
        self.sinaId = [aDecoder decodeObjectForKey:@"13"];
        self.qqNickName = [aDecoder decodeObjectForKey:@"14"];
        self.sinaNickName = [aDecoder decodeObjectForKey:@"15"];
        self.vipStartDate = [aDecoder decodeObjectForKey:@"16"];
        self.vipOverData = [aDecoder decodeObjectForKey:@"17"];
        self.lastSysMessageCount = [aDecoder decodeIntegerForKey:@"18"];
        self.lastMedalCount = [aDecoder decodeIntegerForKey:@"19"];
        self.coins = [aDecoder decodeIntForKey:@"20"];
    }
    return self;
}

@end
